﻿using ETA.Common;
using ETA.Entity;
using ETA.IRepository;
using NHibernate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ETA.Repositories
{
    public class RegistrationMainFormRepository : NhRepositoryBase<RegistrationMainForm, string>, IRegistrationMainFormRepository
    {
        public bool UpdateCurrentStep(string userId, string flowStepId)
        {
            ISession session = NHibernateHelper.GetSession();
            bool IsSuccess = false;

            ITransaction trans = session.BeginTransaction();
            try
            {
                string sql = " update [User] set FlowStepId=:flowStepId,FlowChangeTime=:now where userId=:userId";
                var Query = session.CreateSQLQuery(sql).AddEntity(typeof(User))
                    .SetString("flowStepId", flowStepId)
                    .SetString("userId", userId)
                    .SetDateTime("now", DateTime.Now);
                Query.ExecuteUpdate();
                session.Flush();
                trans.Commit();
                IsSuccess = true;
            }
            catch (Exception ex)
            {
                IsSuccess = false;
                Logger.LogInfo(ex.Message);
                trans.Rollback();
            }
            finally
            {
                if (session != null)
                {
                    session.Clear();
                }
            }
            return IsSuccess;
        }
    }
}
